<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="学院" prop="collegeId">
        <college-selector v-model="queryParams.collegeId"></college-selector>
      </el-form-item>
      <el-form-item label="系部" prop="departmentId">
        <departments-selector
          v-model="queryParams.departmentId"
          :college-id="queryParams.collegeId"
        ></departments-selector>
      </el-form-item>
      <el-form-item label="教室分类" prop="typeId">
        <el-select
          v-model="queryParams.typeId"
          placeholder="请选择教室分类"
          filterable
          clearable
        >
          <el-option
            v-for="dict in classroomCategoryList"
            :key="dict.id"
            :label="dict.typeName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教室名称" prop="classroomName">
        <el-input
          v-model="queryParams.classroomName"
          placeholder="请输入教室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          @click="handleAdd"
          v-hasPermi="['collegebase:classroom-info:create']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          @click="handleFeatures"
          v-hasPermi="['collegebase:classroom-info:create']"
          >添加功能</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['collegebase:classroom-info:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      highlight-current-row
      @row-click="rowClick"
    >
      <el-table-column label width="35">
        <template slot-scope="scope">
          <el-radio :label="scope.row.id" v-model="sourceId"></el-radio>
        </template>
      </el-table-column>
      <!-- <el-table-column label  ="id" align="center" prop="id"  v-if="columns[0].visible"/> -->
      <el-table-column
        label="学院"
        width="110px"
        align="center"
        prop="collegeName"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="系部"
        width="110px"
        align="center"
        prop="departName"
        v-if="columns[1].visible"
      />
      <!-- <el-table-column label="层级" align="center" prop="hierarchy"  v-if="columns[2].visible"/> -->
      <!-- <el-table-column label="班级" align="center" prop="classesId" v-if="columns[3].visible" /> -->
      <el-table-column
        label="教室分类"
        align="center"
        prop="categoryName"
        v-if="columns[2].visible"
      />
      <el-table-column
        label="教室名称"
        align="center"
        prop="classroomName"
        v-if="columns[3].visible"
      />
      <el-table-column
        label="容纳人数"
        align="center"
        prop="galleryful"
        v-if="columns[4].visible"
      />
      <el-table-column
        label="教室位置"
        align="center"
        prop="address"
        v-if="columns[5].visible"
      />
      <el-table-column
        label="功能"
        width="160px"
        align="center"
        prop="fun"
        v-if="columns[6].visible"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <el-tag
            v-for="(item, index) in JSON.parse(scope.row.fun)"
            :key="index"
            style="margin: 5px"
          >
            {{ item.functionName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="165"
        v-if="columns[7].visible"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="教室备注"
        :show-tooltip-when-overflow="true"
        align="center"
        prop="remark"
        v-if="columns[8].visible"
      />
      <el-table-column
        label="操作"
        align="center"
        width="110px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['collegebase:classroom-info:update']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['collegebase:classroom-info:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加修改组件 -->
    <form-edit
      :show.sync="open"
      :fun="classFunctionList"
      :title="title"
      @success="getList"
      ref="editDialog"
    ></form-edit>
    <features :show.sync="showFeatures" :tags="classFunctionList" />
  </div>
</template>

<script>
import {
  createClassroomInfo,
  updateClassroomInfo,
  deleteClassroomInfo,
  getClassroomInfo,
  getClassroomInfoPage,
  exportClassroomInfoExcel, getClassroomCategory,
} from "@/api/collegebase/classroomInfo";
import { getClassroomFunctionPage } from "@/api/collegebase/classroomFunction";
import formEdit from "./formEdit";
import features from "./features";
import common from "@/api/collegebase/common";
import CollegeSelector from "@/components/CollegeSelector";
import DepartmentsSelector from "@/components/DepartmentsSelector";
export default {
  name: "ClassroomInfo",
  components: {
    DepartmentsSelector,
    CollegeSelector,
    formEdit,
    features
  },
  data() {
    return {
      classroomCategoryList: [],
      columns: [
        { key: 0, label: "学院", visible: true },
        { key: 1, label: "系部", visible: true },
        { key: 2, label: "教室分类", visible: true },
        { key: 3, label: "教室名称", visible: true },
        { key: 4, label: "容纳人数", visible: true },
        { key: 5, label: "教室位置", visible: true },
        { key: 6, label: "功能", visible: true },
        { key: 7, label: "创建时间", visible: false },
        { key: 8, label: "教室备注", visible: true },
      ],
      showFeatures: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教室列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        collegeId: null,
        departmentId: null,
        typeId: null,
        classroomName: null,
      },
      sourceId: '',
      classFunctionList: [],
    };
  },
  created() {
    this.getList();
    this.getData();
  },
  methods: {

    async getData() {

      const classroomCategory = await getClassroomCategory();
      this.classroomCategoryList = classroomCategory.data;
    },
    async handleFeatures() {
      this.showFeatures = true;
      const res = await getClassroomFunctionPage({ pageNo: 1, pageSize: 100 })
      res.data.list.forEach(e => {
        e.name = e.functionName;
        e.inputVisible = false;
      })
      this.classFunctionList = res.data.list;


    },
    async getTags() {
      const res = await getClassroomFunctionPage({ pageNo: 1, pageSize: 100 })
      res.data.list.forEach(e => {
        e.name = e.functionName;
        e.inputVisible = false;
      })
      this.classFunctionList = res.data.list;
    },
    rowClick(row) {
      // console.log(row)
      this.sourceId = row.id;
      console.log(this.sourceId)
      // console.log('exportid---', row.snapshotId)

    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getClassroomInfoPage(this.queryParams).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加教室";

      this.$refs.editDialog.initData({});
      this.$refs.editDialog.getData();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id;
      getClassroomInfo(id).then((response) => {
        let form = response.data;
        this.open = true;
        this.title = "修改教室";
        this.$refs.editDialog.initData(form);
        this.$refs.editDialog.getData();
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除教室名称为"' + row.classroomName + '"的数据项?')
        .then(function () {
          return deleteClassroomInfo(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal
        .confirm("是否确认导出所有教室数据项?")
        .then(() => {
          this.exportLoading = true;
          return exportClassroomInfoExcel(params);
        })
        .then((response) => {
          this.$download.excel(response, "教室.xls");
          this.exportLoading = false;
        })
        .catch(() => { });
    },
  },
};
</script>
